<template>
	<common-card type="detail" title="实验安排详情" :loading="loading">
		<detail-body :data="dataDetail"></detail-body>
		<template v-if="dataDetail.type === '2'">
			<div class="data_detail_wrapper">
				<el-row>
					<el-col :span="8">
						<div class="item">
							<div class="label">完成时间：</div>
							<div class="content">{{ dataDetail.endTime }}</div>
						</div>
					</el-col>
					<el-col :span="8">
						<div class="item">
							<div class="label">实际机时：</div>
							<div class="content">{{ dataDetail.machineHour }}</div>
						</div>
					</el-col>
				</el-row>
				<div class="item">
					<div class="label">完成情况：</div>
					<div class="content">{{ dataDetail.content }}</div>
				</div>
				<div class="item">
					<div class="label">实验结果：</div>
					<div class="content">
						<el-button class="down" type="text" v-for="(item, index) in resultFiles" :key="index" @click="handleDownloadFile(item)">{{ item.name }}</el-button>
					</div>
				</div>
			</div>
		</template>
		<template v-else>
			<div class="data_detail_wrapper" v-if="dataDetail.isSave === '1' && dataDetail.audit !== '3'">
				<div class="item">
					<div class="label">审核人：</div>
					<div class="content">{{ dataDetail.auditName }}</div>
				</div>
				<div class="item">
					<div class="label">审核状态：</div>
					<div class="content">
						<el-tag type="success" v-if="dataDetail.audit === '1'">已通过</el-tag>
						<el-tag type="danger" v-if="dataDetail.audit === '2'">已驳回</el-tag>
					</div>
				</div>
				<div class="item">
					<div class="label">审核意见：</div>
					<div class="content">{{ dataDetail.auditDesc }}</div>
				</div>
				<div class="item">
					<div class="label">审核时间：</div>
					<div class="content">{{ dataDetail.auditTime }}</div>
				</div>
				<div class="item" v-if="!!dataDetail.filed6">
					<div class="label">审核签名：</div>
					<div class="image"><img :src="dataDetail.filed6" /></div>
				</div>
			</div>
		</template>
		<template slot="button">
			<template v-if="userRole === 'manager'">
				<template v-if="dataType !== 'summary'">
					<el-button type="success" icon="sw-icon-edit" @click="handleEditData()">编辑</el-button>
				</template>
			</template>
			<template v-else-if="dataType === 'schedule'">
				<el-button type="danger" icon="sw-icon-btn-added" @click="handleAddedData()">新建</el-button>
				<template v-if="dataDetail.isSave === '2'">
					<el-button type="warning" icon="sw-icon-edit" @click="handleEditData()">编辑</el-button>
				</template>
			</template>
			<template v-else-if="dataType === 'scheduleAudit'">
				<template v-if="!!dataDetail.audit && dataDetail.audit === '3'">
					<el-button type="success" icon="sw-icon-btn-audit" v-hasPermi="'c6a9e3c5fdf848d08535f557f87691e7'" @click="handleAuditData()">审核</el-button>
				</template>
			</template>
			<template v-else-if="dataType === 'summary'">
				<el-button type="success" icon="sw-icon-repeal" v-hasPermi="'17da844c47ed4b3c95c0f930ff7de222'" @click="handleReturnSummary()">退回</el-button>
			</template>
		</template>
	</common-card>
</template>

<script>
import detailBody from './body';
import commonCard from '../commonCard';
import { piStatDetail, piStatBack } from '@/api/business/tbPiStat';
export default {
	components: { commonCard, detailBody },
	props: {
		userRole: { type: String, default: '' },
		dataType: { type: String, default: '' }
	},
	data() {
		return {
			loading: false,
			topicData: {},
			dataDetail: {}
		};
	},
	computed: {
		dataId() {
			return this.$route.query.id || '';
		},
		resultFiles() {
			let datas = this.dataDetail.filed5;
			return !datas && this.$isJSON(datas) ? [] : JSON.parse(datas);
		}
	},
	watch: {
		dataId: {
			handler(id) {
				this.loading = true;
				this.getDataDetail(id);
			},
			immediate: true
		}
	},
	methods: {
		//获取数据详情
		async getDataDetail(id) {
			let result = await piStatDetail(id).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				this.dataDetail = this.$detailData(result.data);
			}
		},

		handleAddedData() {
			let query = { type: 'add' };
			this.$router.push({ path: `/${this.userRole}/${this.dataType}/list`, query });
		},

		handleEditData() {
			let query = { type: 'edit', id: this.dataDetail.id };
			this.$router.push({ path: `/${this.userRole}/${this.dataType}/list`, query });
		},

		handleAuditData() {
			let query = { id: this.dataDetail.id };
			this.$router.push({ path: `/${this.userRole}/${this.dataType}/audit`, query });
		},

		//下载文件
		handleDownloadFile(data) {
			this.$fileDownload(data.url, `${data.name}.${data.suffix}`);
		},

		//退回小结
		handleReturnSummary() {
			let that = this;
			that.$confirm('此操作将退回实验提交小结, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.returnSummaryRequest(that.dataId);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		async returnSummaryRequest(id) {
			let params = { id };
			let result = await piStatBack(params).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `退回实验小结成功`);
				this.$router.back();
			}
		}
	}
};
</script>

<style lang="scss" scoped></style>
